using UnityEngine;
using System.Collections;

/// \class  gkGodVsAliensPlayerSphericalMovement
/// \brief 	the player spherical movement controller
[AddComponentMenu("GK/GodVsAliens/Player/gkGodVsAliensPlayerSphericalMovement")]
[RequireComponent(typeof(gkInputAccelerometer))]
[RequireComponent(typeof(gkSphericalPhysicController))]
[RequireComponent(typeof(gkPhysicComponent))]
public class gkGodVsAliensPlayerSphericalMovement : MonoBehaviour
{
	/// The maximum speed
    public float maxSpeed = 5.0f;
	
	/// The sensibility dead zone in axis percent
    public float deadZone = 0.1f;
	
	/// The accelerometer input
	private gkInputAccelerometer m_rInputAccelerometer;

    /// The physic component
    private gkPhysicComponent m_rPhysicComponent;
	
    /// \brief  Called just after the game begin to play
    void Start()
    {
        // Grab input
		m_rInputAccelerometer = GetComponent<gkInputAccelerometer>();

        // Grab the physic component
        m_rPhysicComponent = GetComponent<gkPhysicComponent>();
	}
	
	/// \brief  Called each frame
    void Update()
    {
		Vector3 f3velocity;
		Vector3 f3TiltPercent;
		
		// Get the accelerometer tilt percent
		f3TiltPercent = m_rInputAccelerometer.GetTiltPercent();
		
		if(f3TiltPercent.sqrMagnitude > (deadZone * deadZone))
		{
			// Compute the current velocity accordingly to the current tilt
			f3velocity = Vector3.zero;
			f3velocity.x = -f3TiltPercent.y * maxSpeed;
			f3velocity.z = f3TiltPercent.x * maxSpeed;
			
		    // Apply the velocity
	        m_rPhysicComponent.SetLinearVelocity(f3velocity);
		}
		else
		{
			// Apply the velocity
	        m_rPhysicComponent.SetLinearVelocity(Vector3.zero);
		}
    }
}